﻿AstroRecruitment.ResumeUpload = function () {
    AstroRecruitment.uploader = new qq.FileUploader({
        element: $('#reference-upload').get(0),
        action: '/__utility/ajax/fileupload.ashx',
        sizeLimit: 1024 * 1024 * 4,
        allowedExtensions: ['pdf', 'doc', 'docx', 'rtf', 'txt', 'odf'],
        template: '<div class="qq-uploader">' +
				'<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' +
				'<div class="qq-upload-button"><span class="upload-btn-text">Select a file to upload</span></div>' +
				'<ul class="qq-upload-list"></ul>' +
			    '</div>',
        onSubmit: function (id, fileName) {
            $('#reference-upload span.upload-btn-text, div.references-wrapper div.reference-upload-rules').hide();
            $('div.references-wrapper div.upload-progress-wrapper').show();
            //$('#job_application_submit').attr("disabled", true);
        },
        onProgress: function (id, fileName, loaded, total) {
            var percentage = Math.floor(loaded / total) * 100;
            $('div.references-wrapper div.upload-progress-info').text(percentage + '% complete');
            $('div.references-wrapper div.upload-progress-value').css('width', percentage + '%');
        },
        onComplete: function (id, fileName, responseJSON) {
            if (responseJSON.success) {
                if (responseJSON.webPath && responseJSON.webPath) {
                    var filelink = '<a target="_blank" alt="Click to open your file" title="Click to open your file" href="' + responseJSON.webPath + '">' + responseJSON.filename + '</a>';

                    $('div.references-wrapper div.upload-progress-wrapper').hide();
                    $('div.references-wrapper div.file-holder div.file').html(filelink);
                    $('div.references-wrapper div.file-holder').show();
                    $('#job_application_filename').val(responseJSON.filename);
                    $('#job_application_cv').val(responseJSON.webPath);
                }
            }
        },
        showMessage: function (message) {
            $('#info-popup h3').text('Message');
            Cufon.replace('#info-popup h3', { fontFamily: 'Otari-Bold-Limited' });
            $('#info-popup .content').html(message);
            $('#info-popup a.close').show();
            $('#info-popup').fadeIn();
            $('#fader').fadeIn();
        }
    });

    $('a.delete-file').click(function (e) {
        e.preventDefault();
        $('div.references-wrapper div.file-holder div.file').text('');
        $('div.references-wrapper div.file-holder').hide();
        $('#reference-upload, span.upload-btn-text, div.references-wrapper div.reference-upload-rules').show();

        var current_filename = $('#job_application_current_filename');
        var current_cv = $('#job_application_current_cv');
        if (current_filename != null && current_cv != null) {
            $('#job_application_filename').val(current_filename.val());
            $('#job_application_cv').val(current_cv.val());
        }
        else {
            $('#job_application_filename').val(null);
            $('#job_application_cv').val(null);
        }
    });
};

AstroRecruitment.ResumeUpload();